﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to CustomerHandler.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (CustomerHandler.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Data.SqlClient;
using Custom.Web.Extensions;
using ProfilingApp.Handlers.Messages;
//using MessageAPI.Core.Implementation;

#endregion


namespace ProfilingApp.Handlers
{
    public class CustomerHandler : MethodRoutingJsonHandler
    {
        //private long startTime;

        /// <summary>
        /// Creates the customer.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public CreateCustomerResponse CreateCustomer(CreateCustomerRequest request)
        {
            PerformVoidBehaviour();

            var connection =
                new SqlConnection(
                    "Data Source=.\\sqlexpress;Initial Catalog=ProfilingAppDatabase;Integrated Security=SSPI;");

            connection.Open();

            var createCustomerCommand = connection.CreateCommand();
            createCustomerCommand.CommandText =
                string.Format(
                    "INSERT INTO [ProfilingAppDatabase].[dbo].[CUSTOMER]([TITLE],[NAME],[SURNAME],[EMAIL])" +
                    "VALUES('{0}','{1}','{2}','{3}'); select @@IDENTITY;",
                    request.Title,
                    request.Name,
                    request.Surname,
                    request.EmailAddress);

            var id = createCustomerCommand.ExecuteScalar().ToString();

            connection.Close();

            return new CreateCustomerResponse {CustomerId = id};
        }


        public void PerformVoidBehaviour()
        {
            // Going to throw an exception randomly in order to simulate a real application. Using random numbers to do the job.
            Random random = new Random();
            var randomNumber=random.Next(1, 2);
            if(randomNumber>1)
                throw new ApplicationException();

            string dummy = "somevalue";
        }
    }
}